package com.centronix.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.centronix.bean.ComboBean;
import com.centronix.domain.Product;
import com.centronix.domain.ProductType;
import com.centronix.manager.ProductCatManager;
import com.centronix.manager.ProductManager;
import com.centronix.manager.ProductTypeManager;
import com.centronix.util.WebUtil;

@ManagedBean(name = "productBean")
@SessionScoped
public class ProductBean implements Serializable{

	private static final long serialVersionUID = 1L;

	@ManagedProperty("#{productTypeManager}")
    private ProductTypeManager productTypeManager;
	
	@ManagedProperty("#{productCatManager}")
    private ProductCatManager productCatManager;
	
	@ManagedProperty("#{productManager}")
    private ProductManager productManager;
	
	
	private List<ComboBean> productCatComboList =  new ArrayList<ComboBean>(0);
	private List<ComboBean> productTypeComboList =  new ArrayList<ComboBean>(0);
	
	private Product newProduct = new Product();
	private Product selectedProduct = new Product();
	
	private List<Product> allProduct = new ArrayList<Product>(0);
	
	private boolean dataVisible = false;
	private String selectedProductCat = "";
	private String selectedProductType = "";

	public String findAllProduct(){
		allProduct = productManager.findAllProduct();
		productCatComboList = productCatManager.getProductCatCombo();
		dataVisible = true;
		return "product";
	}
	
	public String loadProductTypeCMB(){
		int catId = Integer.valueOf(selectedProductCat);
		productTypeComboList = productTypeManager.getProductTypeCombo(catId);
		return "";
	}
	
	public String saveNewProduct(){
		int proType = Integer.valueOf(selectedProductType);
		ProductType productType = new ProductType();
		productType.setTypeId(proType);
		newProduct.setProductType(productType);
		productManager.saveProduct(newProduct);
		allProduct = productManager.findAllProduct();
		newProduct = new Product();
		selectedProductCat = "";
		selectedProductType = "";
		productTypeComboList.clear();
		WebUtil.showSuccessfulMessage("Record Saved!");
		return "";
	}
	
	public String updateProduct(){
		productManager.saveProduct(newProduct);
		allProduct = productManager.findAllProduct();
		newProduct = new Product();
		selectedProductCat = "";
		selectedProductType = "";
		productTypeComboList.clear();
		WebUtil.showSuccessfulMessage("Record Updated!");
		return "";
	}
	
	public String deleteProduct(){
		productManager.deleteProduct(selectedProduct.getProductId());
		allProduct = productManager.findAllProduct();
		newProduct = new Product();
		selectedProductCat = "";
		selectedProductType = "";
		productTypeComboList.clear();
		WebUtil.showSuccessfulMessage("Record Deleted!");
		return "";
	}

	public ProductTypeManager getProductTypeManager() {
		return productTypeManager;
	}

	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}

	public ProductCatManager getProductCatManager() {
		return productCatManager;
	}

	public void setProductCatManager(ProductCatManager productCatManager) {
		this.productCatManager = productCatManager;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public List<ComboBean> getProductCatComboList() {
		return productCatComboList;
	}

	public void setProductCatComboList(List<ComboBean> productCatComboList) {
		this.productCatComboList = productCatComboList;
	}

	public List<ComboBean> getProductTypeComboList() {
		return productTypeComboList;
	}

	public void setProductTypeComboList(List<ComboBean> productTypeComboList) {
		this.productTypeComboList = productTypeComboList;
	}

	public Product getNewProduct() {
		return newProduct;
	}

	public void setNewProduct(Product newProduct) {
		this.newProduct = newProduct;
	}

	public Product getSelectedProduct() {
		return selectedProduct;
	}

	public void setSelectedProduct(Product selectedProduct) {
		this.selectedProduct = selectedProduct;
	}

	public List<Product> getAllProduct() {
		return allProduct;
	}

	public void setAllProduct(List<Product> allProduct) {
		this.allProduct = allProduct;
	}

	public boolean isDataVisible() {
		return dataVisible;
	}

	public void setDataVisible(boolean dataVisible) {
		this.dataVisible = dataVisible;
	}

	public String getSelectedProductCat() {
		return selectedProductCat;
	}

	public void setSelectedProductCat(String selectedProductCat) {
		this.selectedProductCat = selectedProductCat;
	}

	public String getSelectedProductType() {
		return selectedProductType;
	}

	public void setSelectedProductType(String selectedProductType) {
		this.selectedProductType = selectedProductType;
	}

	
	

}
